package com.drought.rained.ui.activity.mine;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.drought.rained.R;
import com.drought.rained.adapter.BillAdapterLJ;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.mvpview.BillMvpViewLJ;
import com.drought.rained.mvpview.RolloverAppMvpVipwLJ;
import com.drought.rained.presenter.BillPresenterLJ;
import com.drought.rained.presenter.RolloverAppPresenterLJ;
import com.drought.rained.response.BlankResponseLJ;
import com.drought.rained.response.OrderDetailResponseLJ;
import com.drought.rained.response.OrderItemResponseLJ;
import com.drought.rained.utils.ToastUtilsLJ;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("CheckResult")
@Route(name = "我的账单", path = RouterMapLJ.MINE_BILL)
public class MineBillActivityLJ extends BaseTitleActivity implements BillAdapterLJ.OnBliiDetailListener, BillMvpViewLJ, RolloverAppMvpVipwLJ {

    @BindView(R.id.empty_view)
    TextView emptyView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView billRecycler;
    private BillAdapterLJ mRecyclerAdapter;
    private LinearLayoutManager linearLayoutManager;

    private int mCurrentPage = 0; // 当前页page
    private BillPresenterLJ billPresenter;
    private RolloverAppPresenterLJ rolloverAppPresenter;
    private boolean star; // 展期可配

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        refreshLayout.setEnableLoadmore(true);//开启加载

        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        billRecycler.setLayoutManager(linearLayoutManager);
        billRecycler.setAdapter(getRecyclerAdapter());
        // 下拉刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mCurrentPage = 0;
                getOrderPageList();
            }
        });
        // 上拉加载
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (linearLayoutManager == null) return;
                getOrderPageList();
            }
        });
    }

    @Override
    protected void onUIReady() {
        billPresenter = new BillPresenterLJ(this);
        rolloverAppPresenter = new RolloverAppPresenterLJ(this);
        rolloverAppPresenter.checkCollectStatus();
    }

    public BillAdapterLJ getRecyclerAdapter() {
        if (mRecyclerAdapter == null) {
            mRecyclerAdapter = new BillAdapterLJ(this, this);
        }
        return mRecyclerAdapter;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.bill_page_title);
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_of_mine_bill;
    }

    @Override
    public void onBliiDetailClick(View view, int position) {
        billPresenter.getOrderDetail(getRecyclerAdapter().getData().get(position).getOrderId(), position);
    }

    private void getOrderPageList() {
        star = true;
        mCurrentPage++;
        billPresenter.getOrderPageList(mCurrentPage);
    }

    @Override
    public void onGetDataNext(OrderItemResponseLJ borrowingResponse) {
        dismissLoadingView();
        if (borrowingResponse.getStatus() == ConstantLJ.HTTP_OK) {
            if (borrowingResponse.hasData()) {
                if (emptyView.getVisibility() == View.VISIBLE) {
                    emptyView.setVisibility(View.GONE);
                }
                if (mCurrentPage == 1) {
                    getRecyclerAdapter().updateItems(borrowingResponse.getData());
                    refreshLayout.finishRefresh();
                    if (!refreshLayout.isEnableLoadmore()) {
                        refreshLayout.setEnableLoadmore(true);//重新开启加载
                    }
                } else {
                    getRecyclerAdapter().addItems(borrowingResponse.getData());
                    refreshLayout.finishLoadmore();
                }
            } else {
                mCurrentPage--;
                refreshLayout.setEnableLoadmore(false);//关闭加载
                if (refreshLayout.isRefreshing()) {
                    refreshLayout.finishRefresh();
                } else if(!refreshLayout.isLoadmoreFinished()) {
                    refreshLayout.finishLoadmore();
                }
            }
        } else {
            ToastUtilsLJ.showShort(MineBillActivityLJ.this, borrowingResponse.getMessage());
        }
    }

    @Override
    public void onGetDataNext(OrderDetailResponseLJ orderDetailResponse, final int position) {
        if (orderDetailResponse.getStatus() == ConstantLJ.HTTP_OK) {
            getRecyclerAdapter().setOrderDetail(position,orderDetailResponse.getData());
        } else {
            ToastUtilsLJ.showShort(MineBillActivityLJ.this, orderDetailResponse.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
        if (UserInfoLJ.isLogin() && !star) {
            getOrderPageList();
        }
    }

    @Override
    public void onGetDataNext(BlankResponseLJ response) {
        if (response.getStatus() == ConstantLJ.HTTP_OK) {
            boolean rel = response.isRel();
            getRecyclerAdapter().setRel(rel);
        }
        if (UserInfoLJ.isLogin()) {
            getOrderPageList();
        }
    }
}
